GameEvents.NatsumiSetPlotProperty.Add(function (x, y, key, value)
	local plot = Map.GetPlot(x, y);
	plot:SetProperty(key, value);
end);

local WITCH_TIME_KEY = 'WITCH_TIME';
local WATER_INDEX = GameInfo.Terrains['TERRAIN_COAST'].Index;
function HD_Natsumi_WitchImprove (playerId, unitId)
	local unit = UnitManager.GetUnit(playerId, unitId);
	local time = unit:GetProperty(WITCH_TIME_KEY) or 0;
	unit:SetProperty(WITCH_TIME_KEY, time + 1);
end
GameEvents.HD_Natsumi_WitchImprove.Add(HD_Natsumi_WitchImprove);
GameEvents.HD_Natsumi_WitchImproveSwitch.Add(function (playerId, index)
	local plot = Map.GetPlotByIndex(index);
	if (plot ~= nil) and (plot:GetResourceType() ~= -1) and (plot:GetImprovementType() == -1) and (plot:GetDistrictType() == -1) then
		local resourceInfo = GameInfo.Resources[plot:GetResourceType()];
		local validImprovement = nil;
		if (resourceInfo.ResourceClassType ~= 'RESOURCECLASS_STRATEGIC') then
			local isWater = plot:GetTerrainType() == WATER_INDEX;
			for row in GameInfo.Improvement_ValidResources() do
				local improvementType = row.ImprovementType;
				if row.ResourceType == resourceInfo.ResourceType and
						(((not isWater) and (improvementType == 'IMPROVEMENT_MINE'
						or improvementType == 'IMPROVEMENT_QUARRY'
						or improvementType == 'IMPROVEMENT_FARM'
						or improvementType == 'IMPROVEMENT_PLANTATION'
						or improvementType == 'IMPROVEMENT_CAMP'
						or improvementType == 'IMPROVEMENT_PASTURE'
						or improvementType == 'IMPROVEMENT_LUMBER_MILL'))
						or (isWater and improvementType == 'IMPROVEMENT_FISHING_BOATS')) then
					validImprovement = improvementType;
				end
			end
		end
		if validImprovement then
			ImprovementBuilder.SetImprovementType(plot, GameInfo.Improvements[validImprovement].Index, playerId);
		end
	end
end);

local NATSUMI_WITCH_PENDING_KEY = 'NATSUMI_WITCH_COPY_PENDING';
function HD_Natsumi_WitchCopy (playerId, unitId)
	local player = Players[playerId];
	player:SetProperty(NATSUMI_WITCH_PENDING_KEY, unitId);
end
GameEvents.HD_Natsumi_WitchCopy.Add(HD_Natsumi_WitchCopy);
GameEvents.HD_Natsumi_WitchCopySwitch.Add(function (copiedPlayerId, copiedUnitId, copyingPlayerId, copyingUnitId, greatPersonIndividualId)
	local copyingPlayer = Players[copyingPlayerId];
	copyingPlayer:SetProperty(NATSUMI_WITCH_PENDING_KEY, null);

	-- check conditions
	if copiedPlayerId == copyingPlayerId then
		return;
	end;
	local copiedUnit = UnitManager.GetUnit(copiedPlayerId, copiedUnitId);
	local copyingUnit = UnitManager.GetUnit(copyingPlayerId, copyingUnitId);
	if (copiedUnit == nil) or (copyingUnit == nil) then
		return;
	end
	local location = copyingUnit:GetLocation();
	local targetLocation = copiedUnit:GetLocation();
	if Map.GetPlotDistance(location.x, location.y, targetLocation.x, targetLocation.y) > 1 then
		return;
	end

	-- remove old unit
	UnitManager.Kill(copyingUnit);

	-- generate new unit
	if greatPersonIndividualId ~= nil then
		Game.GetGreatPeople():CreatePerson(copyingPlayerId, greatPersonIndividualId, location.x, location.y);
	else
		local unitInfo = GameInfo.Units[copiedUnit:GetType()];
		local newUnit = UnitManager.InitUnit(copyingPlayerId, unitInfo.UnitType, location.x, location.y);

		-- copy info
		local religion = copiedUnit:GetReligion();
		if religion ~= nil then
			local religionType = religion:GetReligionType();
			if religionType ~= nil then
				newUnit:GetReligion():SetReligionType(religionType);
			end
		end
	end

end);